package com.wave.blog.serve.controller;

import com.mybatisflex.core.paginate.Page;
import com.wave.blog.serve.common.Result;
import com.wave.blog.serve.entity.Article;
import com.wave.blog.serve.service.ArticleService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import java.util.List;

/**
 * 文章控制器
 * 提供文章相关的RESTful API接口
 *
 * @author wave
 */
@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    /**
     * 创建文章
     *
     * @param article 文章对象
     * @return 创建的文章对象
     */
    @PostMapping
    public Result<Article> createArticle(@RequestBody Article article) {
        boolean saved = articleService.save(article);
        if (saved) {
            return Result.success(article);
        }
        return Result.error("创建文章失败");
    }

    /**
     * 更新文章
     *
     * @param id      文章ID
     * @param article 文章对象
     * @return 更新后的文章对象
     */
    @PutMapping("/{id}")
    public Result<Article> updateArticle(@PathVariable Long id, @RequestBody Article article) {
        // 检查文章是否存在
        Article existingArticle = articleService.getById(id);
        if (existingArticle == null) {
            return Result.error("文章不存在");
        }
        
        article.setId(id);
        boolean updated = articleService.updateById(article);
        if (updated) {
            return Result.success(article);
        }
        return Result.error("更新文章失败");
    }

    /**
     * 根据ID获取文章
     *
     * @param id 文章ID
     * @return 文章对象
     */
    @GetMapping("/{id}")
    public Result<Article> getArticleById(@PathVariable Long id) {
        Article article = articleService.getById(id);
        if (article != null) {
            return Result.success(article);
        }
        return Result.error("文章不存在");
    }

    /**
     * 删除文章
     *
     * @param id 文章ID
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteArticle(@PathVariable Long id) {
        // 检查文章是否存在
        Article article = articleService.getById(id);
        if (article == null) {
            return Result.error("文章不存在");
        }
        
        boolean deleted = articleService.removeById(id);
        if (deleted) {
            return Result.success();
        }
        return Result.error("删除文章失败");
    }

    /**
     * 获取所有文章列表
     *
     * @return 文章列表
     */
    @GetMapping("/list")
    public Result<List<Article>> getArticleList() {
        List<Article> articles = articleService.list();
        return Result.success(articles);
    }

    /**
     * 分页查询文章
     *
     * @param pageNum  页码，默认1
     * @param pageSize 每页大小，默认10
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<Page<Article>> getArticlePage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Article> page = articleService.page(new Page<>(pageNum, pageSize));
        return Result.successPage(page);
    }

}